import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/provider/UserInforModel.dart';
import 'package:flutter_juwo/ui/model/account_money_entity.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';
import 'package:provider/provider.dart';

import 'model/user_state_entity.dart';

///************************
/// @auther nb
/// @function 我的余额
/// @创建日期 2019/9/28 10:31
///************************
class MyMoney extends StatefulWidget {
  @override
  _MyMoneyState createState() => _MyMoneyState();
}

class _MyMoneyState extends State<MyMoney> {
  MyPageState ps = MyPageState.LOADING;
  AccountMoneyEntity entity = null;

  UserStateEntity userStateEntity = null;
  int currentPage = 1;
  int totlePage = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: UiUtils.appBar(context, "我的余额"),
      body: this.getBody(),
    );
  }

  getBody() {
    if (ps == MyPageState.LOADING) {
      return LoadingPage();
    }

    if (ps == MyPageState.HAVE_DATA) {
      return this.getContent();
    }

    return Container();
  }

  getData() {
    FormData params = FormData.from({"type": "1"});
    DioManager.getInstance().get(Api.USER_ACCOUNT, params, (json) {
      entity = AccountMoneyEntity.fromJson(json);

      print("获取账户：$json");
      if (null == entity) {
        Application.router.pop(context);
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        return;
      }

      if (entity.code == Api.SUCCESS_CODE) {
        Provider.of<UserInforModel>(context).updateMoney(entity.data.cash);
        currentPage = 1;
        //  getFlow(currentPage);
        getUserInfor();
        return;
      }

      if (entity.code == Api.ERROR_CODE || entity.code == Api.ERROR_CODE_0) {
        ToastUtil.showShortErrToast(entity.message);
        return;
      }
    }, (err) {
      ToastUtil.showShortErrToast(err);
    });
  }

  getContent() {
    List<Widget> list = [];
    list.add(UiUtils.divider(height: 2));
    list.add(this.getTips());
    list.add(this.getTop());
    list.add(UiUtils.divider(height: 10));
    list.add(this.getBottom());
    list.add(UiUtils.divider(height: 1));

    return Container(
      child: Column(
        children: list,
      ),
    );
  }

  getTop() {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          ///可用余额
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
            child: Text(
              "${entity.data.cash}",
              style: TextStyles.custom(
                  60, Color(0xFFff631c), Font.PingFangMedium),
            ),
          ),
          Container(
            child: Text(
              "可用金额（元）",
              style: TextStyles.custom(
                  26, Colours.appC2, Font.PingFangMedium),
            ),
          ),

          ///待返，累计
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setWidth(60)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Container(
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: Text(
                          "${entity.data.cashFrozen}",
                          style: TextStyles.custom(
                              30, Colours.appC2, Font.PingFangMedium),
                        ),
                      ),
                      Container(
                        child: Text(
                          "待返金额（元）",
                          style: TextStyles.custom(
                              26, Colours.appC2, Font.PingFangMedium),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: Text(
                          "${entity.data.sumCash}",
                          style: TextStyles.custom(
                              30, Colours.appC2, Font.PingFangMedium),
                        ),
                      ),
                      Container(
                        child: Text(
                          "累计金额（元）",
                          style: TextStyles.custom(
                              26, Colours.appC2, Font.PingFangMedium),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),

          GestureDetector(
            onTap: () {
              //提现
              if (null != userStateEntity && userStateEntity.data.status == 1) {
                NavigatorUtils.pushResult(context, Routes.cashOut, (result) {
                  print("result");
                  print(result);
                  if (result) {
                    this.getData();
                  }
                });
              } else {
                ToastUtil.showShortErrToast("更新实名信息后提现");
              }
            },
            child: Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil().setWidth(30),
                  ScreenUtil().setWidth(60),
                  ScreenUtil().setWidth(30),
                  ScreenUtil().setWidth(60)),
              width: ScreenUtil().setWidth(750),
              height: ScreenUtil().setWidth(80),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: Colours.appC16,
                  borderRadius: BorderRadius.circular(20)),
              child: Text(
                "提现",
                style: TextStyles.custom(
                    30, Colours.white, Font.PingFangMedium),
              ),
            ),
          )
        ],
      ),
    );
  }

  void getUserInfor() {
    DioManager.getInstance().get(Api.USER_NOTINFORWS, null, (json) {
      userStateEntity = UserStateEntity.fromJson(json);

      if (null == entity) {
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        NavigatorUtils.goBack(context);
        return;
      }
      if (entity.code != 200) {
        ToastUtil.showShortErrToast(entity.message);
        NavigatorUtils.goBack(context);
        return;
      }
      ps = MyPageState.HAVE_DATA;
      setState(() {});
    }, (err) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      NavigatorUtils.goBack(context);
      return;
    });
  }

  Widget getTips() {
    Widget widget;
    if (userStateEntity.data.status == 1) {
      widget = tips1();
    } else if (userStateEntity.data.status == 2) {
      widget = tips2();
    } else if (userStateEntity.data.status == 3) {
      widget = tips3();
    }
    return Container(
      color: Colors.white,
      child: widget,
    );
  }

  Widget tips1() {
    return Container(
      margin: getMargin(),
      height: ScreenUtil().setWidth(120),
      decoration: getDecoration(),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Container(
            child: Text(
              "${userStateEntity.data.identityName}",
              style: getTextStyle(),
            ),
          ),
          Container(
            child: Text(
              "${userStateEntity.data.bankNumber}",
              style: getTextStyle(),
            ),
          ),
          GestureDetector(
            onTap: () {
              gotoNext();
            },
            child: Container(
              child: Text(
                "已实名",
                style: getUnLinStyle(),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget tips2() {
    return Container(
      margin: getMargin(),
      height: ScreenUtil().setWidth(120),
      decoration: getDecoration(),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Container(
              child: Text(
            "您还没有绑定银行卡",
            style: getTextStyle(),
          )),
          GestureDetector(
              onTap: () {
                gotoNext();
              },
              child: Container(
                child: Text(
                  "绑定银行卡",
                  style: getUnLinStyle(),
                ),
              )),
          Container(
              child: Text(
            "后可提现",
            style: getTextStyle(),
          ))
        ],
      ),
    );
  }

  Widget tips3() {
    return Container(
      margin: getMargin(),
      height: ScreenUtil().setWidth(120),
      decoration: getDecoration(),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Container(
              child: Text(
            "您的实名信息不全，请",
            style: getTextStyle(),
          )),
          GestureDetector(
              onTap: () {
                gotoNext();
              },
              child: Container(
                child: Text(
                  "更新",
                  style: getUnLinStyle(),
                ),
              )),
          Container(
              child: Text(
            "后提现",
            style: getTextStyle(),
          ))
        ],
      ),
    );
  }

  getDecoration() {
    return BoxDecoration(
        borderRadius: BorderRadius.circular(0), color: Colors.amberAccent[700]);
  }

  getTextStyle() {
    return TextStyles.custom(30, Colours.white, Font.PingFangRegular);
  }

  getUnLinStyle() {
    return TextStyle(
        color: Colors.white,
        fontFamily: Font.PingFangRegular,
        decoration: TextDecoration.underline,
        fontSize: ScreenUtil().setSp(30));
  }

  void gotoNext() {
    NavigatorUtils.pushResult(context, Routes.realNameInfor, (ret) {
      if (ret) {
        getData();
      }
    });
  }

  Widget getBottom() {
    return GestureDetector(
      onTap: () {
        NavigatorUtils.push(context, Routes.moneyFlow);
      },
      child: Container(
        padding: EdgeInsets.all(ScreenUtil().setWidth(30)),
        alignment: Alignment.centerLeft,
        color: Colors.white,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(
              "流水记录",
              style: TextStyles.custom(
                  32, Colours.appC16, Font.PingFangRegular),
            ),
            Image.asset(
              Utils.getImgPath("icon_right_01"),
              width: ScreenUtil().setWidth(40),
            )
          ],
        ),
      ),
    );
  }

  getMargin() {
    return EdgeInsets.all(ScreenUtil().setWidth(0)); //30
  }
}
